package com.amigo.online.provider.user.util;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.message.entity.MIMCJsonResponse;
import com.amigo.online.common.base.message.enums.MIMCUrlEnum;
import com.amigo.online.common.base.message.util.MIMCHttpUtil;
import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.param.result.LocalUserInfo;
import com.amigo.online.common.param.result.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class IMGroupUtil {

    /**
     * 修改群信息消息
     * @param topicName
     * @return
     */
    public static ResponseEntity<MIMCJsonResponse> updateGroupInfo(Long topicId, String topicName, String ownerAccount, String extra){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("topicName", topicName);
        jsonObject.put("extra", extra);
        jsonObject.put("ownerAccount", ownerAccount);
        Long userId = LocalUser.getUser().getUser().getId();
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(jsonObject, MIMCHttpUtil.getUserHttpHeaders(userId.toString()));
        RestTemplate restTemplate =new RestTemplate();
        ResponseEntity<MIMCJsonResponse> responseEntity =  restTemplate.exchange(MIMCUrlEnum.UPDATE_GROUP_INFO.getValue()+ topicId, HttpMethod.PUT, requestEntity , MIMCJsonResponse.class);
        log.debug("修改群信息消息返回值: " + responseEntity.getBody());
        return responseEntity;
    }

    /**
     * 发送退群消息
     * @param topicId
     * @return
     */
    public static ResponseEntity<MIMCJsonResponse>  quitGroupMessage(Long groupId,Long uId){
        if(uId==null){
            uId = LocalUser.getUser().getUser().getId();
        }
        RestTemplate restTemplate =new RestTemplate();
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(null, MIMCHttpUtil.getUserHttpHeaders(uId.toString()));
        String quitGroup = MIMCUrlEnum.QUIT_GROUP.getValue();
        ResponseEntity<MIMCJsonResponse> responseEntity =  restTemplate.exchange(quitGroup.replaceFirst("\\$topicId", groupId.toString()), HttpMethod.DELETE, requestEntity , MIMCJsonResponse.class);
        log.debug("发送退群消息返回值: " + responseEntity.getBody());
        return responseEntity;
    }
    public static void main(String[] args) {
        LocalUserInfo localUserInfo = new LocalUserInfo();
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1l);
        localUserInfo.setUser(userInfo);
        LocalUser.setUser(localUserInfo);
        System.out.println(null  + "asda");

    }

}
